#include "file.h"

// 打开
void My_File::open(const char *filename, const char *mode)
{
    this->fd = fopen(filename, mode);
}

// 判定是否打开成功
bool My_File::is_open()
{
    return this->fd != NULL;
}

// 关闭
void My_File::close()
{
    if (this->is_open())
    {
        fclose(this->fd);
        this->fd = NULL;
    }
    else
    {
        cout << "文件未打开或已关闭" << endl;
    }
}

void My_File::operator<<(const char *buf)
{
    if (!this->is_open())
    {
        cout << "打开失败" << endl;
        return ;
    }
    if (buf == NULL)
    {
        cout << "字符串为null" << endl;
        return ;
    }

    fputs(buf , this->fd);

}

void My_File::operator<<(const string &str)
{
    if (!this->is_open())
    {
        cout << "打开失败" << endl;
        return ;
    }


    fputs( str.c_str() , this->fd);
}

void My_File::operator<<(My_File &File)
{
    if (!File.is_open())
    {
        cout << "传入的文件未打开" << endl;
        return ;
    }

    char ch;
    while (EOF != (ch = fgetc(File.fd)))
    {
        fputc(ch , this->fd);
    }
    
}

void My_File::operator>>(char *buf)
{

}

void My_File::operator>>(string &str)
{

}

void My_File::seek(long offset, int origin)
{
    /*
#define SEEK_SET	0   从文件头
#define SEEK_CUR	1   从当前位置
#define SEEK_END	2   从末尾
    */
    if (!this->is_open())
    {
        cout << "打开失败" << endl;
        return ;
    }

    fseek( this->fd , offset , origin);
}
